<!--

    Copyright © 2016-2025 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<form [formGroup]="formGroup" style="min-width: 600px;">
  <mat-toolbar class="flex flex-row" color="primary">
    <h2 translate>ws-client.connections.properties</h2>
    <span class="flex flex-1"></span>
    <button mat-icon-button
      [mat-dialog-close]="null"
      type="button"
      matTooltip="{{ 'action.close' | translate }}"
      matTooltipPosition="above">
      <mat-icon class="material-icons">close</mat-icon>
    </button>
  </mat-toolbar>
  @if (isLoading$ | async) {
    <mat-progress-bar color="warn" mode="indeterminate">
    </mat-progress-bar>
  }
  @if (!(isLoading$ | async)) {
    <div style="height: 4px;"></div>
  }
  <div mat-dialog-content>
    <section class="flex flex-col gap-4">
      <section class="slider flex flex-row items-center gap-2">
        <mat-slide-toggle formControlName="payloadFormatIndicator">
          {{ 'ws-client.last-will.payload-format-indicator' | translate }}
        </mat-slide-toggle>
        <mat-icon class="tb-mat-20" style="color: rgba(0,0,0,0.18)"
          [matTooltip]="'ws-client.last-will.payload-format-indicator-hint' | translate"
          matTooltipPosition="right">
        info</mat-icon>
      </section>
      <mat-form-field class="mat-block" subscriptSizing="dynamic">
        <mat-label translate>ws-client.last-will.content-type</mat-label>
        <input matInput formControlName="contentType">
        <mat-icon matSuffix [matTooltip]="'ws-client.last-will.content-type-hint' | translate">
          help
        </mat-icon>
      </mat-form-field>
      <section class="flex flex-row gap-2">
        <mat-form-field class="flex flex-1" subscriptSizing="dynamic">
          <mat-label translate>ws-client.last-will.message-expiry-interval</mat-label>
          <input matInput formControlName="messageExpiryInterval" type="number" min="0" [max]="calcMax('messageExpiryIntervalUnit')">
          <mat-icon matSuffix [matTooltip]="'ws-client.last-will.message-expiry-interval-hint' | translate">
            help
          </mat-icon>
        </mat-form-field>
        <mat-form-field class="flex flex-1" subscriptSizing="dynamic">
          <mat-label translate>timeunit.units</mat-label>
          <mat-select formControlName="messageExpiryIntervalUnit">
            @for (unit of timeUnitTypes; track unit) {
              <mat-option [value]="unit">
                {{ timeUnitTypeTranslationMap.get(unit) | translate }}
              </mat-option>
            }
          </mat-select>
        </mat-form-field>
      </section>
      <mat-form-field class="mat-block" subscriptSizing="dynamic">
        <mat-label translate>ws-client.connections.topic-alias</mat-label>
        <input matInput type="number" min="1" formControlName="topicAlias">
        <mat-icon matSuffix [matTooltip]="'ws-client.connections.topic-alias-hint' | translate">
          help
        </mat-icon>
        @if (formGroup.get('topicAlias').errors?.topicAliasMaxError) {
          <mat-error>
            {{ 'ws-client.connections.topic-alias-error' | translate }}
          </mat-error>
        }
      </mat-form-field>
      <mat-form-field class="flex flex-1" subscriptSizing="dynamic">
        <mat-label translate>ws-client.last-will.correlation-data</mat-label>
        <input matInput formControlName="correlationData">
        <mat-icon matSuffix [matTooltip]="'ws-client.last-will.correlation-data-hint' | translate">
          help
        </mat-icon>
      </mat-form-field>
      <mat-form-field class="mat-block" subscriptSizing="dynamic">
        <mat-label translate>ws-client.last-will.response-topic</mat-label>
        <input matInput formControlName="responseTopic">
        <mat-icon matSuffix [matTooltip]="'ws-client.last-will.response-topic-hint' | translate">
          help
        </mat-icon>
      </mat-form-field>
      <tb-user-properties [entity]="props?.userProperties"
        [mqttVersion]="5"
        [reset]="resetForm"
        formControlName="userProperties">
      </tb-user-properties>
    </section>
  </div>
  <div mat-dialog-actions class="flex flex-row items-center justify-end">
    <button mat-button color="primary"
      type="button"
      [disabled]="(isLoading$ | async)"
      (click)="reset()">
      {{ 'action.reset' | translate }}
    </button>
    <button mat-button color="primary"
      type="button"
      [disabled]="(isLoading$ | async)"
      [mat-dialog-close]="null" cdkFocusInitial>
      {{ 'action.cancel' | translate }}
    </button>
    <button mat-raised-button color="primary"
      (click)="save()"
      [disabled]="(isLoading$ | async) || formGroup?.invalid || !formGroup?.dirty">
      {{ 'action.save' | translate }}
    </button>
  </div>
</form>
